// 简单工厂模式
#include <iostream>

using namespace std;

enum class CTYPE { COREA, COREB };

class SingleCore {  // 抽象类
public:
    virtual void show() = 0;
    virtual ~SingleCore() = default;
};

class SingleCoreA : public SingleCore {  // 具体类
public:
    SingleCoreA() { cout << "build SingleCoreA\n"; }
    void show() { cout << "SingleCore A\n"; }
};

class SingleCoreB : public SingleCore {  // 具体类
public:
    SingleCoreB() { cout << "build SingleCoreB\n"; }
    void show() { cout << "SingleCore B\n"; }
};

class SimpleFactory {  // 简单工厂类
public:
    SingleCore* createSingleCore(const CTYPE ctype) {
        if (ctype == CTYPE::COREA) {
            core_ = new SingleCoreA();
            return core_;
        } else if (ctype == CTYPE::COREB) {
            core_ = new SingleCoreB();
            return core_;
        } else {
            return nullptr;
        }
    }
    ~SimpleFactory() {
        if (core_ != nullptr) {
            delete core_;
            core_ = nullptr;
        }
    }

private:
    SingleCore* core_ = nullptr;
};

int main() {
    SimpleFactory factory;
    SingleCore* coreA = factory.createSingleCore(CTYPE::COREA);
    coreA->show();
    SingleCore* coreB = factory.createSingleCore(CTYPE::COREB);
    coreB->show();
    return 0;
}